﻿using Snowflake.Core;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace homework9
{
    public partial class Form1 : Form
    {
        public string StartUrl { get; set; }
        public int MaxPage { get; set; }
        public Crawler MyCrawler { get; set; } = new Crawler();
        public Form1()
        {
            InitializeComponent();
            MaxPage = 50;
            txtWeb.DataBindings.Add("Text", this, "StartUrl");
            StartUrl = "http://www.cnblogs.com/dstang2000/";
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            this.txtProcess.Clear();
            this.txtProcess.AppendText("爬虫开始" + "\r\n");
            if (StartUrl == null)
            {
                MessageBox.Show("请输入需要爬取的URL");
                return;
            }
            MyCrawler.Urls = new Hashtable();
            MyCrawler.waiting.Clear();
            MyCrawler.pageInfo.Clear();
            MyCrawler.waiting.Enqueue(StartUrl);
            while (MyCrawler.Urls.Count < MaxPage && MyCrawler.waiting.Count > 0)
            {
                string current = MyCrawler.waiting.Dequeue();
                try
                {
                    string html = MyCrawler.DownLoad(current);
                    MyCrawler.Urls[current] = true;
                    //解析
                    MyCrawler. Parse(html, current);
                    MyCrawler.pageInfo[current] = "success";
                }
                catch
                {
                    MyCrawler.pageInfo[current] = "fail";
                }
            }
            foreach (string key in MyCrawler.pageInfo.Keys)
            {
                string info = $"[url={key}, status={MyCrawler.pageInfo[key]}]";
                this.txtProcess.AppendText(info + "\r\n");
            }
            this.txtProcess.AppendText("爬虫结束" + "\r\n");
        }
    }
}
